USE [BS1]
GO

CREATE FUNCTION dbo.Proper_Case (
    @string varchar(2000)
    )
RETURNS varchar(2000)
AS
--Sample use: SELECT dbo.Proper_Case('aBC mike;a alkj/as 12k 43')
BEGIN

DECLARE @previousByte int
DECLARE @byte int

SET @string = LOWER(@string)
IF LEFT(@string, 1) LIKE '[a-z]'
    SET @string = STUFF(@string, 1, 1, UPPER(LEFT(@string, 1)))
SET @previousByte = 2

WHILE 1 = 1
BEGIN
    SET @byte = PATINDEX('%[^a-z0-9][a-z]%', SUBSTRING(@string, @previousByte, 2000))
    --PRINT 'Byte = ' + CAST(@byte AS varchar(10))
    IF @byte = 0
        BREAK
    SET @string = STUFF(@string, @previousByte + @byte, 1, 
        UPPER(SUBSTRING(@string, @previousByte + @byte, 1)))
    SET @previousByte = @previousByte + @byte + 1
    --PRINT 'PrevByte = ' + CAST(@previousByte AS varchar(10))
END --WHIILE

RETURN @string

END --FUNCTION

GO